package com.jyw.business.de.expand.task;

import cn.hutool.core.util.StrUtil;
import com.jyw.business.de.constant.DemandStateConstant;
import com.jyw.business.de.constant.TaskStateConstant;
import com.jyw.business.de.domain.DeDemandAllocation;
import com.jyw.business.de.domain.DeDemandInfo;
import com.jyw.business.de.domain.DeTaskInfo;
import com.jyw.business.de.service.IDeDemandAllocationService;
import com.jyw.business.de.service.IDeDemandInfoService;
import com.jyw.business.de.service.IDeTaskInfoService;
import com.jyw.business.de.service.ISysDemandReminderConfigService;
import com.jyw.common.constant.CacheConstants;
import com.jyw.common.core.redis.RedisCache;
import com.jyw.common.utils.DateUtils;
import com.jyw.system.domain.vo.SysDemandReminderConfigVo;
import com.jyw.system.enums.MessageTypeEnum;
import com.jyw.system.service.ISysMessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * 需求任务超时未接收提醒
 */
@Component("DeTaskAcceptTask")
@Slf4j
public class DeTaskAcceptTask {

    @Resource
    private IDeTaskInfoService taskInfoService;

    @Resource
    private ISysDemandReminderConfigService configService;

    @Resource
    private ISysMessageService messageService;

    @Resource
    private RedisCache redisCache;

    public void remind(){
        SysDemandReminderConfigVo config = configService.getDefaultConfig();
        //超时未分配开关
        if (config.getTaskAcceptTimeoutRemind()) {
            Date timeOut = DateUtils.addMinutes(DateUtils.getNowDate(), -config.getTaskAcceptTimeoutMinute());
            List<DeTaskInfo> list = taskInfoService.lambdaQuery()
                    .eq(DeTaskInfo::getTaskState, TaskStateConstant.WAIT_ACCEPT)
                    .le(DeTaskInfo::getWaitAcceptTime, timeOut)
                    .list();
            list.forEach(data->{
                if(!redisCache.hasKey(CacheConstants.DE_TASK_WAIT_ACCEPT_KEY+data.getId())){
                    if(config.getTaskAcceptTimeoutReminder().contains("1")){
                        //通知处理人
                        messageService.sendSysMessage(
                                MessageTypeEnum.SYS_DEMAND.getValue(),
                                data.getTaskUser(),
                                StrUtil.format("您有一条任务【{}】，超时未接收，请尽快处理",data.getTaskName())
                        );
                    }
                    if(config.getTaskAcceptTimeoutReminder().contains("2")){
                        //通知创建人
                        messageService.sendSysMessage(
                                MessageTypeEnum.SYS_DEMAND.getValue(),
                                data.getCreateUser(),
                                StrUtil.format("您创建的任务【{}】，超时未接收，请尽快处理",data.getTaskName())
                        );
                    }
                    redisCache.setCacheObject(CacheConstants.DE_TASK_WAIT_ACCEPT_KEY+data.getId(),data,config.getTaskAcceptTimeoutInterval(),TimeUnit.MINUTES);
                }
            });
        }
    }
}
